package newui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.widget.Button;
import android.widget.TextView;

import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw.activity.HTML5WebView;
import com.shwread.android.qysw.activity.NewBookDetailActivity;
import com.shwread.android.qysw10000243.R;

import logic.bean.BookInfo;
import logic.constants.DefaultConsts;
import logic.util.logutil.LogUtil;


public class BookListWebActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	private HTML5WebView mWebView;
	private TextView     tvTitle = null;
	private Button       btnBack = null;
	private String       mTitle  = "";
	private String       mUrl    = "";


	private Handler myHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			switch (msg.what) {
				case 1:
					String bookId = bundle.getString("bookId");
					String name = bundle.getString("bookName");
					String type = bundle.getString("bookType");
					gotoBookDetail(bookId, name, type);
					break;

				default:
					break;
			}
		}

	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mWebView = new HTML5WebView(this);
		
		Bundle bundle = getIntent().getExtras();
		mTitle = bundle.getString(DefaultConsts.title_s);
		mUrl = bundle.getString(DefaultConsts.req_url_s);
		if (savedInstanceState != null)
		{
			mWebView.restoreState(savedInstanceState);
		}
		else
		{
			mWebView.loadUrl(mUrl);
		}
		
		setContentView(mWebView.getLayout());
		initView();
		setListener();
	}
	
	private void initView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText(mTitle);
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		addJavaScriptMap(new SubMaintenanceJavaScriptInterface(), "qyreader");
	}
	
	private void setListener() {
		btnBack.setOnClickListener(this);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mWebView.saveState(outState);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		mWebView.stopLoading();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (mWebView.inCustomView())
			{
				mWebView.hideCustomView();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public Handler getUIHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
    public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				finish();
				break;
		}
    }

	private void sendToastMessage(String bookId, String bookName, String bookType) {

		Message msg = Message.obtain();
		msg.what = 1;
		Bundle bundle = new Bundle();
		bundle.putString("bookId", bookId);
		bundle.putString("bookName", bookName);
		bundle.putString("bookType", bookType);
		msg.setData(bundle);
		myHandler.sendMessage(msg);
	}

	private void gotoBookDetail(String bookId, String name, String bookType) {
		BookInfo info = new BookInfo();
		info.setBookId(Long.parseLong(bookId));
		info.setName(name);
		info.setContent_type(bookType);
		//#ifdef SHOW_OLDBOOKDETAIL
		//$ Intent intent = new Intent(this, BookDetailActivity.class);
		//#else
		Intent intent = new Intent(this, NewBookDetailActivity.class);
		//#endif
		intent.putExtra("BookInfo", info);
		startActivity(intent);

	}

	final class SubMaintenanceJavaScriptInterface {

		/**
		 * 在屏幕下端提示用户
		 */
		@JavascriptInterface
		public void setBookInfo(String bookId, String bookName, String bookType) {
			LogUtil.d("--", "bookId：" + bookId + " | bookName:" + bookName + " |bookType:" + bookType);
			sendToastMessage(bookId, bookName, bookType);
		}

		/**
		 * 提供给B端打印日志
		 */
		public void fireLog(String log) {
			LogUtil.d("dddd", log);
		}

		/**
		 * 假如有历史记录 回退历史记录 否则退出容器
		 */
		public void goBack() {
			BaseBusinessActivity.uiHandler.post(new Runnable() {
				@Override
				public void run() {
					onBackPressed();
				}
			});
		}
	}

	public void addJavaScriptMap(Object obj, String objName) {
		mWebView.addJavascriptInterface(obj, objName);
	}
}
